<template>
  <div>
    <el-dialog :title="ruleForm.id == '' ? '添加鱼池投喂量' : '编辑鱼池投喂量'" :visible.sync="addEditIsDialog_c" @close="resetForm('formName')" width="800px">
      <el-form :model="ruleForm" :rules="rules" ref="formName" label-width="120px" class="demo-ruleForm" :class="{ 'form-rules-show': seeType }">

        <el-row>
          <el-col :span="12">
            <el-form-item label="软颗粒投喂量：" prop="softValue">
              <el-input v-model="ruleForm.softValue" placeholder="请输入软颗粒" clearable show-word-limit></el-input>
              <!-- <el-select v-model="ruleForm.code" placeholder="请选择鱼池编码" clearable>
                <el-option v-for="item in fishArray" :key="item.id" :label="item.name" :value="item.code"></el-option>
              </el-select> -->
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="硬颗粒投喂量：" prop="value">
              <el-input v-model="ruleForm.value" placeholder="请输入软颗粒" clearable show-word-limit></el-input>
              <!-- <el-select v-model="ruleForm.name" placeholder="请选择鱼池名称" clearable>
                <el-option v-for="item in fishArray" :key="item.id" :label="item.name" :value="item.name"></el-option>
              </el-select> -->
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('formName')">取 消</el-button>
        <el-button class="btn-light-blue" v-reduceExpenditure-button @click="submitForm('formName')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script type="text/ecmascript-6">
import { getDataHisFeeder, getObjFishTypes, saveDataHisFeeder, getObjFishponds } from '@/api/breeding'
import { getCompanyInfoTree } from '@/api/company'
import { getSysCompanyBases, getSysFishTypes } from '@/api/system'

export default {
  components: {},
  props: {
    addEditId: {
      type: String,
      default: ''
    },
    addEditStatus: {
      type: String,
      default: 'add'
    },
    addEditIsDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      ruleForm: {
        // 表单字段
        id: '',
        companyId: '',
        baseId: '',
        code: '',
        diameter: '',
        name: '',
        row: '',
        sqn: '',
        state: '',
        ste: '',
        cdepcode: ''
      },
      rules: {
        // 表单规则
        companyId: [{ required: true, message: '请选择公司名称', trigger: 'change' }],
        baseId: [{ required: true, message: '请选择基地', trigger: 'change' }],
        code: [{ required: true, message: '请输入鱼池编码', trigger: 'change' }],
        diameter: [{ required: true, message: '请输入卵径', trigger: 'change' }],
        name: [{ required: true, message: '请输入鱼池名称', trigger: 'change' }],
        row: [{ required: true, message: '请输入排名称', trigger: 'change' }],
        sqn: [{ required: true, message: '请输入平方米', trigger: 'change' }],
        state: [{ required: true, message: '请选择鱼池状态', trigger: 'change' }],
        ste: [{ required: true, message: '请输入立方米', trigger: 'change' }]
      },
      fishArray: [], //全部鱼池
      addEditIsDialog_c: false, // 弹窗是否显示
      seeType: false, // 是否禁用
      baseArray: [], // 基地集合
      companyArray: [], // 公司集合
      fishTypeArray: [], // 鱼类集合
      userType: '', // 用户类型（0超级管理员，1公司管理员）
      fishStatus: [
        {
          value: 0,
          label: '正常'
        },
        {
          value: 1,
          label: '空池'
        },
        {
          value: 2,
          label: '停食'
        },
        {
          value: 3,
          label: '加工前'
        },
        {
          value: 4,
          label: '消毒'
        },
        {
          value: 5,
          label: '盘点'
        }
      ]
    }
  },
  watch: {
    addEditIsDialog(val) {
      this.addEditIsDialog_c = val
      if (this.addEditIsDialog_c) {
        // 添加
        if (this.addEditStatus == 'add') {
          this.ruleForm.id = this.addEditId
          this.ruleForm.code = ''
          this.ruleForm.name = ''
          this.seeType = false
        }

        // 编辑
        if (this.addEditStatus == 'edit') {
          this.ruleForm.id = this.addEditId
          this.seeType = false
          this.getDetail()
        }

        // 查看
        if (this.addEditStatus == 'see') {
          this.ruleForm.id = this.addEditId
          this.seeType = true
          this.getDetail()
        }

        this.userType = Number(window.localStorage.getItem('userType'))

        // 获取公司列表
        this.getCompanyInfoTree()

        this.getSysCompanyBases()
      }
    }
  },
  methods: {
    // 详情
    getDetail() {
      getDataHisFeeder({ id: this.addEditId }).then((res) => {
        if (res.code == 200) {
          this.ruleForm = res.data

          // 获取公司列表
          this.getCompanyInfoTree()

          this.getSysCompanyBases()
        }
      })
    },
    // 选择基地后请求数据
    ChangeBase() {
      console.log(1)
      this.getObjFishponds()
    },
    //获取全部鱼池
    getObjFishponds() {
      getObjFishponds(this.ruleForm).then((res) => {
        if (res.code == 200) {
          this.fishArray = res.data
        }
      })
    },
    // 提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          saveDataHisFeeder(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: res.msg
              })
              // 关闭弹窗
              this.$parent.closeAddEdit()
              // 刷新列表
              this.$parent.onSubmit()
            }
          })
        }
      })
    },
    // 取消
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.$parent.closeAddEdit()
    },
    // 获取公司列表
    getCompanyInfoTree() {
      if (this.ruleForm.userType === '') {
        return
      }

      if (this.ruleForm.userType == 0) {
        // 超级管理员，公司只有一个总公司
        this.companyArray = [
          {
            id: 0,
            name: '总公司',
            parentId: 0
          }
        ]
      } else {
        getCompanyInfoTree({}).then((res) => {
          if (res.code == 200) {
            this.companyArray = res.data
          }
        })
      }
    },
    // 公司下拉切换
    changeTreeItem(val) {
      if (val) {
        this.ruleForm.companyId = val[val.length - 1]
      } else {
        this.ruleForm.companyId = ''
      }

      this.ruleForm.baseId = ''
      this.baseArray = []

      // 获取基地列表
      this.getSysCompanyBases()
    },
    // 获取基地列表
    getSysCompanyBases() {
      if (this.ruleForm.companyId === '' || this.ruleForm.companyId == null) {
        return
      }

      getSysCompanyBases({ id: this.ruleForm.companyId }).then((res) => {
        if (res.code == 200) {
          this.baseArray = res.data
        }
      })
    },
    getObjFishTypes() {
      getObjFishTypes({ id: this.ruleForm.companyId }).then((res) => {
        if (res.code == 200) {
          this.fishTypeArray = res.data
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.demo-ruleForm {
  width: 95%;
  margin: 0 auto;
  padding-top: 10px;
}

.el-cascader {
  width: 100%;
}

.el-select {
  width: 100%;
}
</style>
